package com.siemens.industrial.web.mvc.controller;

import com.siemens.industrial.model.admin.Device;
import com.siemens.industrial.utils.UUIDGenerator;
import com.siemens.industrial.web.mvc.dao.admin.DeviceDao;
import com.siemens.industrial.web.mvc.dao.partner.PartnerDeviceDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by weig on 8/23/16.
 */
@Controller
public class HomeController {
    private static final Logger logger = LogManager.getLogger(HomeController.class.getName());

    @Resource
    DeviceDao deviceDao;

    @Resource
    PartnerDeviceDao partnerDeviceDao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getHomePage() {
        logger.trace("trace message " + 0);
        logger.debug("debug message " + 1);
        logger.info("info message " + 2);
        logger.warn("warn message " + 3);
        logger.error("error message " + 4);
        logger.fatal("fatal message " + 5);

        return "partner/user/test";
    }

    @RequestMapping(value = "/buildTestData", method = RequestMethod.GET)
    public String generateData() {
        for (int i = 0; i < 20; i++) {
            Device d = new Device();
            d.setDeviceId(UUIDGenerator.getUUID());
            d.setDeviceUniqueId(UUIDGenerator.getUUID());
            d.setServiceAddress("http://192.168.1.14:8080/api/v1/sendHeart");
            d.setTenantId("ca42f726-df93-414f-a28e-7e041c527af7");

            deviceDao.save(d);

            Device dd = new Device();
            dd.setDeviceId(UUIDGenerator.getUUID());
            dd.setDeviceUniqueId(UUIDGenerator.getUUID());
            dd.setServiceAddress("http://192.168.1.14:8080/api/v1/sendHeart");
            dd.setTenantId("bfcbab7b-6667-4971-94ea-cf15341aa3f1");

            deviceDao.save(dd);
        }
        return "index";
    }

    @RequestMapping(value = "/api/v1/heart", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getHeart() {
        return new HashMap<String, Object>() {{
            put("time", new Date().getTime());
        }};
    }
}
